<?php
/**
 * Created by PhpStorm.
 * User: 73640
 * Date: 2018/10/19
 * Time: 9:59
 */

namespace App\Repository;


use App\City;
use Overtrue\Pinyin\Pinyin;

class CityRepository
{
    /**
     * @return array
     */
    public function getList():array {
        $pinyin=new Pinyin();
        $city= City::where('parent_id',0)->get();
        $cityList=[];
        foreach ($city as $k=>$v){
            $cityList[$k]['id']=$v->id;
            $cityList[$k]['title']=$v->title;
            $cityList[$k]['is_hot']=$v->is_hot;
            $cityList[$k]['addr']=strtoupper(substr($pinyin->abbr($v->title),0,1));
        }
        return $cityList;
    }

    /**
     * @param int $city_id
     * @return City
     */
    public function details(int $city_id):City{
        $city=   City::with(['children'=>function($query){
            $query->with('children');
        }])->where('id',$city_id)->first();
        return $city;
    }

}